var passport = require('passport');
var gameRoutes = require('./gameRoutes');
var utils = require('../utils');
var noOp = utils.noOp;

exports.setIo = function(i) {
  io = i;
  gameRoutes.setIo(i);
};

exports.setModels = function(m) {
  models = m;
  gameRoutes.setModels(m);
};

exports.index = function(req, res) {
  req.user.displayFormat(function(err, player){
    res.render('home/index.html',
        {player: player,
         csrfToken: req.session._csrf
        });
  });
};

exports.login = function(req, res) {
  res.render('home/login.html');
};

exports.queryGames = function(req, res) {
  queryGames = [];
  models.Game.all(function(err, games) {
    function pushGame(games, index) {
      index = index || 0;
      if (index >= games.length) {
        res.send(queryGames);
        return;
      }
      games[index].displayFormat(function(err, game){
        queryGames.push(game);
        pushGame(games, index + 1);
      });
    }
    pushGame(games);
  });
};

exports.logout =  function(req, res) {
  req.logout();
  res.redirect('/');
};